<?php
namespace app\home\controller;

use app\common\model\TopicMessage as TopicMessageModel;
use app\common\model\Topic as TopicModel;
use app\common\model\Topiclike as TopiclikeModel;
use app\common\model\Member as MemberModel;

/**
 * 资讯评论控制器
 * News Classes
 * @package app\index\controller
 */
class Topicmessage extends Base
{
    /**
     * [index  消息列表]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new TopicMessageModel;
        $tmodel = new TopicModel;
        $lmodel = new TopiclikeModel;
        $mmodel = new MemberModel;
        $this->getPageAndSize($param);
        $user = $this->user;
        $map = [];
        try {
            $map['c.mid'] = $user['id'];
            $counts = $model->getCount($map);
            $allpage = intval(ceil($counts / $this->size));//总页面
            $lists = $model->getlist($map, $this->from, $this->size,'c.id,c.cid,c.type,c.create_time,c.is_read');
            foreach ($lists as &$v) {
                if($v['type'] == 1){
                    $comment = $tmodel->findData($v['cid'],'id','parid,content,comment_uid,states');
                    $v['comment'] = urldecode($comment['content']);
                    $comments = $tmodel->findData($comment['parid'],'id','id,parid,content,states');
                    $v['comments'] = urldecode($comments['content']);
                    $mem = $mmodel->findData($comment['comment_uid'],'id','username,img');
                    $http = substr($mem['img'],0,4);
                    if($http != 'http'){
                        $mem['img'] = config('domain_name').$mem['img'];
                    }
                    $v['username'] = $mem['username'];
                    $v['img'] = $mem['img'];
                    $v['commentid'] = $comments['id'];
                }elseif($v['type'] == 2){
                    $like = $lmodel->findData($v['cid'],'id','topic_id,memid');
                    $mem = $mmodel->findData($like['memid'],'id','username,img');
                    $http = substr($mem['img'],0,4);
                    if($http != 'http'){
                        $mem['img'] = config('domain_name').$mem['img'];
                    }
                    $v['username'] = $mem['username'];
                    $v['img'] = $mem['img'];
                    $comment = $tmodel->findData($like['topic_id'],'id','id,parid,content');
                    $v['comments'] = urldecode($comment['content']);
                    if($comment['parid'] == 0){
                        $v['commentid'] = $comment['id'];
                    }else{
                        $v['commentid'] = $comment['parid'];
                    }
                    $v['like_num'] = $lmodel->getAllCount(['topic_id'=>$like['topic_id']]);
                    $v['like_num'] = $v['like_num'] - 1;
                }
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功', [
            'count' => $counts,//总记录数
            'allpage' => $allpage,
            'lists' => $lists,
        ]);
    }
    /**
     * [read  阅读消息]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function read()
    {
        $param = input('param.');
        $model = new TopicMessageModel();
        try{
            $model->editData(['id'=>$param['id'],'is_read'=>2]);
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '失败' . $e->getMessage());
        }
        return admin_json(1, '成功');
    }
}